package com.example.peibei.ui.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.request.RequestOptions;
import com.dingtao.common.bean.GetOrderDetail;
import com.dingtao.common.core.DataCall;
import com.dingtao.common.core.WDActivity;
import com.dingtao.common.core.exception.ApiException;
import com.dingtao.common.util.GlideUtils;
import com.dingtao.common.util.SPUtils;
import com.dingtao.common.util.UIUtils;
import com.example.peibei.R;
import com.example.peibei.base.SpConstant;
import com.example.peibei.service.presenter.ConfirmOrderStatusPresenter;
import com.example.peibei.service.presenter.GetOrderDetailPresenter;
import com.example.peibei.ui.weight.Callback;
import com.example.peibei.ui.weight.ExpandTextView;
import com.tencent.imsdk.v2.V2TIMConversation;
import com.tencent.qcloud.tuicore.TUIConstants;
import com.tencent.qcloud.tuicore.TUICore;
import com.tencent.qcloud.tuikit.tuiconversation.bean.ConversationInfo;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class MyGetOrderDetailActivity extends WDActivity {
    @BindView(R.id.ll_chat)
    LinearLayout ll_chat;
    @BindView(R.id.iv_one)
    ImageView iv_one;
    @BindView(R.id.view_line_one)
    View view_line_one;
    @BindView(R.id.iv_three)
    ImageView iv_two;
    @BindView(R.id.iv_two)
    ImageView iv_three;
    @BindView(R.id.view_line_two)
    View view_line_two;
    @BindView(R.id.iv_head)
    ImageView iv_head;
    @BindView(R.id.tv_name)
    TextView tv_name;
    @BindView(R.id.tv_address)
    TextView tv_address;
    @BindView(R.id.iv_cate)
    TextView iv_cate;
    @BindView(R.id.tv_dec)
    TextView tv_dec;
    @BindView(R.id.tv_start_time)
    TextView tv_start_time;
    @BindView(R.id.tv_start_date)
    TextView tv_start_date;
    @BindView(R.id.tv_max)
    TextView tv_max;
    @BindView(R.id.tv_number)
    TextView tv_number;
    @BindView(R.id.tv_money)
    TextView tv_money;
    @BindView(R.id.btn_cancel)
    Button btn_cancel;
    @BindView(R.id.btn_submit)
    Button btn_submit;
    @BindView(R.id.btn_evaluate)
    Button btn_evaluate;
    @BindView(R.id.ll_btn_complete)
    LinearLayout ll_btn_complete;
    @BindView(R.id.iv_img_one)
    ImageView iv_img_one;
    @BindView(R.id.iv_img_two)
    ImageView iv_img_two;
    @BindView(R.id.iv_img_three)
    ImageView iv_img_three;
    @BindView(R.id.ll_imgs)
    LinearLayout ll_imgs;

    private String consumeOrderId;
    private String mid;
    private String nickName;
    private int status;
    private GetOrderDetailPresenter getOrderDetailPresenter;
    private SPUtils spUtils;
    private String token;
    private ConfirmOrderStatusPresenter confirmOrderStatusPresenter;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_my_get_order_detail;
    }

    @Override
    protected void initView() {
        spUtils = new SPUtils(this, SpConstant.USER_TOKEN);
        token = spUtils.getString(SpConstant.USER_TOKEN);
        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
        consumeOrderId = bundle.getString("consumeOrderId");
        Log.i("answer", "订单id:" + consumeOrderId);
        mid = bundle.getString("userId");
        nickName = bundle.getString("nickName");
        status = bundle.getInt("status");
        if (status == 0) {
            view_line_one.setBackgroundResource(R.drawable.shape_line_unchecked);
            view_line_two.setBackgroundResource(R.drawable.shape_line_unchecked);
            iv_two.setBackgroundResource(R.mipmap.pay_sure_no);
            iv_three.setBackgroundResource(R.mipmap.pay_sure_no);
            btn_cancel.setVisibility(View.VISIBLE);
            btn_submit.setVisibility(View.GONE);
            ll_btn_complete.setVisibility(View.GONE);
        } else if (status == 1) {
            view_line_two.setBackgroundResource(R.drawable.shape_line_unchecked);
            iv_three.setBackgroundResource(R.mipmap.pay_sure_no);
            btn_cancel.setVisibility(View.GONE);
            ll_btn_complete.setVisibility(View.GONE);
            btn_submit.setVisibility(View.VISIBLE);
        } else if (status == 4) {
            btn_cancel.setVisibility(View.GONE);
            btn_submit.setVisibility(View.GONE);
            ll_btn_complete.setVisibility(View.VISIBLE);
            btn_evaluate.setText("去追评");
        } else {
            btn_cancel.setVisibility(View.GONE);
            btn_submit.setVisibility(View.GONE);
            ll_btn_complete.setVisibility(View.VISIBLE);
        }
//        tv_dec.setMaxLineCount(3);
//        tv_dec.setCollapseText("收起");
//        tv_dec.setExpandText("详情");
//        tv_dec.setCollapseEnable(true);
        getOrderDetailPresenter = new GetOrderDetailPresenter(new OrderPswCall());
        getOrderDetailPresenter.reqeust("Bearer " + token, consumeOrderId, "1");
        confirmOrderStatusPresenter = new ConfirmOrderStatusPresenter(new ChangeOrderStatusCall());

        findViewById(R.id.user_ll).setVisibility(View.VISIBLE);
    }

    @OnClick(R.id.iv_back)
    public void back() {
        finish();
    }

    @OnClick(R.id.btn_cancel)
    public void cancel() {
        Bundle bundle = new Bundle();
        bundle.putString("consumeOrderId", consumeOrderId);
        bundle.putString("token", token);
        intent(CancelOrderActivity.class, bundle);
        finish();
    }

    @OnClick(R.id.btn_get_more)
    public void getMoreOrder() {
        Bundle bundle = new Bundle();
        bundle.putString("publishOrderId", consumeOrderId + "");
        intent(OrderDetailActivity.class, bundle);
        finish();
    }

    @OnClick(R.id.iv_head)
    public void toPersonInfo() {
        Bundle bundle = new Bundle();
        bundle.putString("mid", mid);
        intent(PersonInfoActivityTwo.class, bundle);
    }

    @OnClick(R.id.btn_evaluate)
    public void evaluate() {
        Bundle bundle = new Bundle();
        bundle.putString("token", token);
        bundle.putString("consumeOrderId", consumeOrderId + "");
        intent(EvaluateActivity.class, bundle);
        finish();
    }

    @OnClick(R.id.btn_submit)
    public void submit() {
        showSubmitDialog();
    }

    private void showSubmitDialog() {
        confirmOrderStatusPresenter.reqeust("Bearer " + token, consumeOrderId);
    }

    @OnClick(R.id.ll_chat)
    public void chat() {
        ConversationInfo conversationInfo = new ConversationInfo();
        conversationInfo.setType(ConversationInfo.TYPE_CUSTOM);
        conversationInfo.setId(mid);
        conversationInfo.setGroup(false);
        conversationInfo.setTitle(nickName);
        startChatActivity(conversationInfo);
//        ContactUtils.startChatActivity(mid, ContactItemBean.TYPE_C2C, "nickname", "");
    }

    private void startChatActivity(ConversationInfo conversationInfo) {
        Bundle param = new Bundle();
        param.putInt(TUIConstants.TUIChat.CHAT_TYPE, conversationInfo.isGroup() ? V2TIMConversation.V2TIM_GROUP : V2TIMConversation.V2TIM_C2C);
        param.putString(TUIConstants.TUIChat.CHAT_ID, conversationInfo.getId());
        param.putString(TUIConstants.TUIChat.CHAT_NAME, conversationInfo.getTitle());
        if (conversationInfo.getDraft() != null) {
            param.putString(TUIConstants.TUIChat.DRAFT_TEXT, conversationInfo.getDraft().getDraftText());
            param.putLong(TUIConstants.TUIChat.DRAFT_TIME, conversationInfo.getDraft().getDraftTime());
        }
        param.putBoolean(TUIConstants.TUIChat.IS_TOP_CHAT, conversationInfo.isTop());

        if (conversationInfo.isGroup()) {
            param.putString(TUIConstants.TUIChat.FACE_URL, conversationInfo.getIconPath());
            param.putString(TUIConstants.TUIChat.GROUP_TYPE, conversationInfo.getGroupType());
        }
        if (conversationInfo.isGroup()) {
            TUICore.startActivity(TUIConstants.TUIChat.GROUP_CHAT_ACTIVITY_NAME, param);
        } else {
            TUICore.startActivity(TUIConstants.TUIChat.C2C_CHAT_ACTIVITY_NAME, param);
        }
    }

    class OrderPswCall implements DataCall<GetOrderDetail> {

        @Override
        public void success(GetOrderDetail data, Object... args) {
            if (data != null) {
                Glide.with(MyGetOrderDetailActivity.this).load(data.getImgUrl()).apply(RequestOptions.bitmapTransform(new CircleCrop())).into(iv_head);
                tv_name.setText(data.getNickName());
                tv_address.setText(data.getArea());
                iv_cate.setText(data.getCategory());
                tv_dec.setText(data.getOrderContent());
//                tv_dec.setCloseText(data.getOrderContent());
                String[] s = data.getBeginTime().split(" ");
                tv_start_date.setText(s[0]);
                tv_start_time.setText(s[0]);
                tv_max.setText(data.getDuration());
                tv_number.setText(data.getConsumeOrderId());
                tv_money.setText(data.getPrice());
//                tv_dec.setText(data.getOrderContent(), false, new Callback() {
//                    @Override
//                    public void onExpand() {
//
//                    }
//
//                    @Override
//                    public void onCollapse() {
//
//                    }
//
//                    @Override
//                    public void onLoss() {
//
//                    }
//
//                    @Override
//                    public void onExpandClick() {
//                        tv_dec.setChanged(true);
//                    }
//
//                    @Override
//                    public void onCollapseClick() {
//                        tv_dec.setChanged(false);
//                    }
//                });
//                List<String> imgs = data.getImgs();
//                if (imgs.size() > 0) {
//                    ll_imgs.setVisibility(View.VISIBLE);
//                    if (imgs.get(0) != null && !TextUtils.isEmpty(imgs.get(0))) {
////                Glide.with(OrderDetailActivity.this).load(imgs.get(0)).into(iv_img_one);
//                        GlideUtils.loadImgTwo(MyGetOrderDetailActivity.this, imgs.get(0), 20, iv_img_one);
//                    }
//                    if (imgs.size() > 1) {
//                        if (imgs.get(1) != null && !TextUtils.isEmpty(imgs.get(1))) {
////                    Glide.with(OrderDetailActivity.this).load(imgs.get(1)).into(iv_img_two);
//                            GlideUtils.loadImgTwo(MyGetOrderDetailActivity.this, imgs.get(1), 20, iv_img_two);
//                        }
//                    }
//                    if (imgs.size() > 2) {
//                        if (imgs.get(2) != null && !TextUtils.isEmpty(imgs.get(2))) {
////                    Glide.with(OrderDetailActivity.this).load(imgs.get(2)).into(iv_img_three);
//                            GlideUtils.loadImgTwo(MyGetOrderDetailActivity.this, imgs.get(2), 20, iv_img_three);
//                        }
//                    }
//                } else {
//                    ll_imgs.setVisibility(View.GONE);
//                }
            }
        }

        @Override
        public void fail(ApiException e, Object... args) {
        }
    }

    class ChangeOrderStatusCall implements DataCall<GetOrderDetail> {

        @Override
        public void success(GetOrderDetail data, Object... args) {
            UIUtils.showToastSafe("订单已成功");
            view_line_two.setBackgroundResource(R.drawable.shape_line);
            iv_two.setBackgroundResource(R.mipmap.pay_sure);
            iv_three.setBackgroundResource(R.mipmap.pay_sure);
            btn_submit.setVisibility(View.GONE);
        }

        @Override
        public void fail(ApiException e, Object... args) {
            UIUtils.showToastSafe(e.getDisplayMessage());
        }
    }

    @Override
    protected void destoryData() {

    }
}
